package com.cskaoyan.market.aspect;

import com.cskaoyan.market.annotation.FlashList;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketFootprintMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.service.FlashMapFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Aspect
public class TriggerUpdateFlashAspect {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    FlashMapFactory flashMapFactory;

    @Autowired
    MarketFootprintMapper footprintMapper;

    @Pointcut("@annotation(com.cskaoyan.market.annotation.FlashList)")
    public void pt(){}

    @Before("pt()")
    public Object before(JoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        FlashList annotation = signature.getMethod().getAnnotation(FlashList.class);
        String before = annotation.runningType();
        String footPrint = annotation.method();
        if("before".equals(before)&&"footPrint".equals(footPrint)){
            boolean footprintsIsFlash = (boolean) flashMapFactory.getValue("footprintsIsFlash");

            if (footprintsIsFlash) {
                MarketUser user = (MarketUser) flashMapFactory.getValue("user");
                Integer userId = user.getId();
                MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
                marketFootprintExample.createCriteria().andUserIdEqualTo(userId);
                List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(marketFootprintExample);
                flashMapFactory.putValue("flashFootprintsList",marketFootprints);
                flashMapFactory.putValue("footprintsIsFlash",false);
            }
        }
        return null;
    }

    @After("pt()")
    public Object after(JoinPoint joinPoint)throws Throwable{
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        FlashList annotation = signature.getMethod().getAnnotation(FlashList.class);
        String after = annotation.runningType();
        String order = annotation.method();
        if("after".equals(after)&&"order".equals(order)||"login".equals(after)){
            MarketUser user = (MarketUser) flashMapFactory.getValue("user");
            Integer userId = user.getId();
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            marketOrderExample.createCriteria().andUserIdEqualTo(userId);
            List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
            flashMapFactory.putValue("flashOrderList",marketOrders);
            flashMapFactory.putValue("orderListIsFlash",false);
        }

        return null;
    }

}
